public class ReplacePanel : SearchPanel
{
	protected Gtk.Entry replace_pharse_entry;

	public string replace_pharse
	{
		get { return this.replace_pharse_entry.get_text (); }
		set { this.replace_pharse_entry.set_text (value);   } 
	}

	public ReplacePanel (ref Gtk.SourceBuffer source_buff, ref Gtk.SourceView source_view)
	{
		base (ref source_buff, ref source_view);

		Gtk.Label replace_pharse_entry_lbl = new Gtk.Label.with_mnemonic ("<b>_Zastąp:</b>");
		replace_pharse_entry_lbl.set_use_markup (true);	
		this.replace_pharse_entry = new Gtk.Entry ();
		replace_pharse_entry_lbl.set_mnemonic_widget (this.replace_pharse_entry);
		
		this.attach (replace_pharse_entry_lbl, 3, 4, 0, 1, Gtk.AttachOptions.SHRINK, Gtk.AttachOptions.SHRINK, 5, 0);
		this.attach_defaults (this.replace_pharse_entry, 4, 5, 0, 1);		
		
		this.replace_pharse_entry.key_press_event += this.on_replace;
		this.replace_pharse_entry.changed += this.on_replace_pharse_entry_changed;		
		if (this.source_buff.get_tag_table().lookup ("selection") == null)
		{		
			weak Gtk.TextTag selection_tag = this.source_buff.create_tag ("selection");	
			
			Gdk.Color c = this.source_view.get_style().base[0];
			Utils.neg_color (ref c);
			selection_tag.background = c.to_string ();
			
			c = this.source_view.get_style().fg[0];
			Utils.neg_color (ref c);
			selection_tag.foreground = c.to_string ();
		}
	}
	
	protected override bool on_search (Gtk.Entry sender, Gdk.EventKey event)
	{		
		if (Gdk.keyval_name(event.keyval) == "Tab" || Gdk.keyval_name(event.keyval) == "Return")
		{
			if (this.search_pharse.length == 0)
			{
				Utils.set_widget_warrnig_background (ref this.pharse_entry);
			}
			else
			{
				this.replace_pharse_entry.grab_focus ();
			}
			return true;
		}
		
		return false;
	}
	
	protected virtual bool on_replace (Gtk.Entry sender, Gdk.EventKey event)
	{
		if (Gdk.keyval_name (event.keyval) == "Return")
		{			
			if (this.replace_pharse.length == 0)
			{
				Utils.set_widget_warrnig_background (ref this.replace_pharse_entry);
				if (this.search_pharse .length == 0)
				{
					Utils.set_widget_warrnig_background (ref this.pharse_entry);
					this.pharse_entry.grab_focus ();									
				}
				return true;
			}		
		
			base.on_search (sender, event);	
		
			if (this.founded.length () > 0)
			{
				do
				{
					Gtk.TextIter start_pos; Gtk.TextIter end_pos;						
					this.source_buff.get_iter_at_mark (out start_pos, this.founded.nth_data (this.founded_pos));
					this.source_buff.get_iter_at_mark (out end_pos, this.founded.nth_data (this.founded_pos+1));

					this.source_buff.apply_tag_by_name ("selection", start_pos, end_pos);

					Gtk.MessageDialog msg = new Gtk.MessageDialog (
						null,
						0,
						Gtk.MessageType.QUESTION,
						Gtk.ButtonsType.NONE,
						"Czy chcesz zastąpić zaznaczone tekst ?"
					);
					msg.add_button (Gtk.STOCK_YES, Gtk.ResponseType.YES);
					msg.add_button (Gtk.STOCK_NO, Gtk.ResponseType.NO);			
					msg.add_button (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL);
					msg.set_default_response (Gtk.ResponseType.YES);					
					int response = msg.run ();					
					msg.destroy ();					
					
					this.source_buff.remove_tag_by_name ("selection", start_pos, end_pos);
					
					if (response == Gtk.ResponseType.YES)
					{												
						this.source_buff.delete (start_pos, end_pos);
						//Zmienil sie tekst, uaktualniamy itera...						
						this.source_buff.get_iter_at_mark (out start_pos, this.founded.nth_data (this.founded_pos));
						this.source_buff.insert (start_pos, this.replace_pharse, (int) this.replace_pharse.length);	
					}
					else if (response == Gtk.ResponseType.CANCEL)
					{
						this.close ();
						return true;
					}		
																		
					this.goto_next_match ();								
				}
				while (this.founded_pos < this.founded.length () - 2);
			}
			else 
			{
				this.pharse_entry.grab_focus ();
			}
		}
		return false;
	}
	
	protected void on_replace_pharse_entry_changed ()
	{
		this.replace_pharse_entry.modify_base (Gtk.StateType.NORMAL, null);
	}
}
